package com.sc.test;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.sc.test.bean.News;
import com.sc.test.bean.URLS;
import com.sc.test.utils.AppUrls;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Shen Chao on 2017-12-27-027.
 */

public class RetrofitRxjavaActivity extends AppCompatActivity {

    @BindView(R.id.lv_news)
    ListView lvNews;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit_rxjava);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bt_getNews, R.id.bt_getNews1, R.id.bt_getNews3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_getNews:
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(URLS.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .build();
                AppUrls appUrls = retrofit.create(AppUrls.class);
                appUrls.getNews("type", URLS.JUHE_APPKEY)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<News>() {
                            @Override
                            public void accept(News news) throws Exception {
                                final List<News.ResultBean.DataBean> dataBeans = news.getResult().getData();
                                lvNews.setAdapter(new BaseAdapter() {
                                    @Override
                                    public int getCount() {
                                        return dataBeans.size();
                                    }

                                    @Override
                                    public Object getItem(int position) {
                                        return null;
                                    }

                                    @Override
                                    public long getItemId(int position) {
                                        return 0;
                                    }

                                    @Override
                                    public View getView(int position, View convertView, ViewGroup parent) {
                                        TextView tv = new TextView(RetrofitRxjavaActivity.this);
                                        tv.setText(dataBeans.get(position).getTitle());
                                        return tv;
                                    }
                                });
                            }
                        });
                break;
            case R.id.bt_getNews1:
                final List<String> list = new ArrayList<>();
                list.add("aaa");
                list.add("bbb");
                list.add("ccc");
                lvNews.setAdapter(new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return list.size();
                    }

                    @Override
                    public Object getItem(int position) {
                        return null;
                    }

                    @Override
                    public long getItemId(int position) {
                        return 0;
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        TextView tv = new TextView(RetrofitRxjavaActivity.this);
                        tv.setText(list.get(position));
                        return tv;
                    }
                });
                break;
            case R.id.bt_getNews3:
                //todo
                break;
        }
    }
}
